<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef"  v-show="showSearch" label-width="80px">
      <!-- <el-form-item label="资产编号" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入资产编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-row>
        <el-col :span="5">
          <el-form-item label="资产名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入资产名称"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col style="margin-left: 10px;" :span="5">
          <el-form-item label="资产状态" prop="status">
            <el-select v-model="queryParams.status"  placeholder="请选择资产状态" clearable>
              <el-option
                v-for="dict in asset_status"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>



      <!-- <el-form-item label="资产类别" prop="classifyId">
        <el-input
          v-model="queryParams.classifyId"
          placeholder="请输入资产类别"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="type">
        <el-input
          v-model="queryParams.type"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="金额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计量单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入计量单位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品序列号" prop="snNum">
        <el-input
          v-model="queryParams.snNum"
          placeholder="请输入产品序列号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来源" prop="sourceId">
        <el-select v-model="queryParams.sourceId" placeholder="请选择来源" clearable>
          <el-option
            v-for="dict in asset_source"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="购入时间" style="width: 308px">
        <el-date-picker
          v-model="daterangePurchaseTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item> -->

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manage:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="资产编号" align="center" prop="num" />
      <el-table-column label="资产状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="asset_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="资产名称" align="center" prop="name" />
      <el-table-column label="资产类别" align="center" prop="classifyId" />
      <el-table-column label="资产照片" align="center" prop="photo" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.photo" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="规格型号" align="center" prop="type" />
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="计量单位" align="center" prop="unit" />
      <el-table-column label="SN号" align="center" prop="snNum" />
      <el-table-column label="使用公司" align="center" prop="companyId" />
      <el-table-column label="使用部门" align="center" prop="deptId" />
      <el-table-column label="创建人" align="center" prop="createUser" />
      <el-table-column label="区域" align="center" prop="region" />
      <el-table-column label="存放地点" align="center" prop="place" />
      <el-table-column label="来源" align="center" prop="sourceId">
        <template #default="scope">
          <dict-tag :options="asset_source" :value="scope.row.sourceId"/>
        </template>
      </el-table-column>
      <el-table-column label="购入时间" align="center" prop="purchaseTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.purchaseTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="备注" align="center" prop="remark" />

      <el-table-column width="130" label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['manage:info:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:info:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资产入库对话框 -->
    <el-dialog :title="title" v-model="open" width="1200px" append-to-body>
      <el-form ref="infoRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <h3>基本信息</h3>
            <el-divider />
          </el-col>
        </el-row>
        
        <el-row>
          <el-col :span="8">
            <el-form-item label="资产名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入资产名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="资产类别" prop="classifyId">
              <el-input v-model="form.classifyId" placeholder="请输入资产类别" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="资产编编码" prop="num">
              <el-input v-model="form.num" placeholder="系统自动生成" disabled />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="规格型号" prop="type">
              <el-input v-model="form.type" placeholder="请输入规格型号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="SN号" prop="snNum">
              <el-input v-model="form.snNum" placeholder="请输入产品序列号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="计量单位" prop="unit">
              <el-input v-model="form.unit" placeholder="请输入计量单位" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="来源" prop="sourceId">
              <el-select v-model="form.sourceId" placeholder="请选择来源" clearable >
                <el-option
                  v-for="dict in asset_source"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="购入时间" prop="purchaseTime">
              <el-date-picker clearable
                v-model="form.purchaseTime"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择购入时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="金额" prop="amount">
              <el-input v-model="form.amount" placeholder="请输入金额" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="区域" prop="region">
              <el-input v-model="form.region" placeholder="请输入区域" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所属公司" prop="companyId">
              <el-input v-model="form.companyId" placeholder="请输入所属公司" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="管理员" prop="createUser">
              <el-input v-model="form.createUser" placeholder="请输入创建人" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <h3>使用信息</h3>
            <el-divider />
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="区域" prop="region">
              <el-input v-model="form.region" placeholder="请输入区域" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所属公司" prop="companyId">
              <el-input v-model="form.companyId" placeholder="请输入所属公司" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="使用部门" prop="deptId">
              <el-input v-model="form.deptId" placeholder="请输入所属部门" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="存放地点" prop="place">
              <el-input v-model="form.place" placeholder="请输入存放地点" />
            </el-form-item>
          </el-col>
            <el-col :span="8" v-if="form.id">
              <el-form-item label="资产状态" prop="status">
                <el-select v-model="form.status" placeholder="请选择资产状态">
                  <el-option
                    v-for="dict in asset_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <h3>其他信息</h3>
            <el-divider />
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="资产照片" prop="photo">
              <image-upload v-model="form.photo"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注">
          <editor v-model="form.remark" :min-height="192"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Info">
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/manage/info";

const { proxy } = getCurrentInstance();
const { asset_source, asset_status } = proxy.useDict('asset_source', 'asset_status');

const infoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangePurchaseTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    num: null,
    status: null,
    name: null,
    classifyId: null,
    type: null,
    amount: null,
    unit: null,
    snNum: null,
    sourceId: null,
    purchaseTime: null,
  },
  rules: {
    num: [
      { required: true, message: "资产编号不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "资产状态不能为空", trigger: "change" }
    ],
    name: [
      { required: true, message: "资产名称不能为空", trigger: "blur" }
    ],
    classifyId: [
      { required: true, message: "资产类别不能为空", trigger: "blur" }
    ],
    type: [
      { required: true, message: "规格型号不能为空", trigger: "blur" }
    ],
    amount: [
      { required: true, message: "金额不能为空", trigger: "blur" }
    ],
    unit: [
      { required: true, message: "计量单位不能为空", trigger: "blur" }
    ],
    snNum: [
      { required: true, message: "产品序列号不能为空", trigger: "blur" }
    ],
    companyId: [
      { required: true, message: "所属公司不能为空", trigger: "blur" }
    ],
    deptId: [
      { required: true, message: "所属部门不能为空", trigger: "blur" }
    ],
    place: [
      { required: true, message: "存放地点不能为空", trigger: "blur" }
    ],
    sourceId: [
      { required: true, message: "来源不能为空", trigger: "change" }
    ],
    purchaseTime: [
      { required: true, message: "购入时间不能为空", trigger: "blur" }
    ],
    createUser: [
      { required: true, message: "创建人不能为空", trigger: "blur" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    region: [
      { required: true, message: "区域不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询资产入库列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangePurchaseTime && '' != daterangePurchaseTime) {
    queryParams.value.params["beginPurchaseTime"] = daterangePurchaseTime.value[0];
    queryParams.value.params["endPurchaseTime"] = daterangePurchaseTime.value[1];
  }
  listInfo(queryParams.value).then(response => {
    infoList.value = response.data;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    num: null,
    status: null,
    name: null,
    classifyId: null,
    photo: null,
    type: null,
    amount: null,
    unit: null,
    snNum: null,
    companyId: null,
    deptId: null,
    place: null,
    sourceId: null,
    purchaseTime: null,
    createUser: null,
    createTime: null,
    remark: null,
    region: null
  };
  proxy.resetForm("infoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangePurchaseTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加资产入库";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getInfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改资产入库";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["infoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除资产入库编号为"' + _ids + '"的数据项？').then(function() {
    return delInfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/info/export', {
    ...queryParams.value
  }, `info_${new Date().getTime()}.xlsx`)
}

getList();
</script>
